package com.nengchuan.kbms.dfs.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nengchuan.config.exception.NcException;
import com.nengchuan.kbms.common.PageServiceImpl;
import com.nengchuan.kbms.dfs.constant.BaseConstant;
import com.nengchuan.kbms.dfs.entity.po.CommonDic;
import com.nengchuan.kbms.dfs.dao.CommonDicMapper;
import com.nengchuan.kbms.dfs.service.CommonDicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nengchuan.util.NullCheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author abe
 * @since 2020-04-24
 */
@Service
@Slf4j
public class CommonDicServiceImpl extends PageServiceImpl<CommonDicMapper, CommonDic> implements CommonDicService {

    /**
     * TODO add cache
     */
    @Override
    public String getName(String typeCode, String code) {
        // 2020-4-24 17:45:41 文件类型暂时不使用数据库中的 name
        if (BaseConstant.TypeCode.EXT_TYPE.equals(typeCode)) {
            return code;
        }
        // ------------------------------------------------
        if (StringUtils.isBlank(typeCode)) {
            throw new NcException("类型编码不能为空");
        }
        if (StringUtils.isBlank(code)) {
            throw new NcException("编码不能为空");
        }
        QueryWrapper<CommonDic> wrapper = new QueryWrapper<>();
        wrapper.lambda().select(CommonDic::getName)
                .eq(CommonDic::getTypeCode, typeCode)
                .eq(CommonDic::getCode, code);
        CommonDic dic = getOne(wrapper, false);
        if (dic == null) {
            log.warn("typeCode: {}, code: {} 不存在", typeCode, code);
            return "";
        }
        return dic.getName();
    }
}
